package com.zero.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zero.dao.StorageDao;
import com.zero.entity.Result;
import com.zero.entity.Storage;
import com.zero.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * (Storage)表服务实现类
 *
 * @author makejava
 * @since 2022-04-24 18:02:20
 */

@Service
public class StorageServiceImpl extends ServiceImpl<StorageDao, Storage>  implements StorageService {

    @Autowired
    private StorageDao storageDao;

    /**
     * 扣除库存
     * @param goodsId 商品id
     * @param count 扣除数量
     * @return 库存扣除结果
     */
    @Override
    @Transactional
    public Result deductStorage(int goodsId, int count) {
        Integer storageCount = storageDao.queryStorageCountById(goodsId);
        if (storageCount == null) {
            return new Result(3, "商品id不存在！");
        }
        if (storageCount >= count) {
            Integer deductResult = storageDao.deductStorage(goodsId, count);
            if (deductResult == 1) {
                return new Result(1, "库存扣除成功！");
            }
        }
        return new Result(2, "库存不足！");
    }
}

